<template>
	<!--股东信息  -->
	<div>
		<FormPanel
			ref="FormPanel"
			:list="initForm"
			:cols="2"
			:formType="formType"
			:labelWidth="150"
			:isSubmitBtn="false"
			@formChange="formChangeHandle"
			class="mtop"
		/>
	</div>
</template>

<script>
	import { dictionary } from '@/utils/dictMixin';
	import * as types from '@/api/cms-api/origin/originInfo/types';

	import vm from './bridge';

	export default {
		mixins: [dictionary],
		model:{
			prop:'pdata',
			event:'click'
		},
		props: {
			pdata: {
				type: Object,
				default: () => ({})
			},
			formType:{
				type: String,
				required:true
			}
		},
		data() {
			return {
				initForm: this.initFormFun(),
			};
		},
		mounted(){
			//注册经销商类型变化时触发的事件
			//在审核时，给此页面的类型为只读，但是基本信息（infoBase可以编辑，会显示会触发事件，事件会触发禁用取消，覆盖整个表单的类型（表单类型为show，但是依旧可以输入，（部分）））
			if(this.formType != "show"){
				vm.$on('changeDealerType',this.changeDealerType);
			}
		},
		methods: {
			//初始化数据
			init() {
				this.initForm.forEach(x => {
					x.initialValue = this.pdata[x.fieldName];
				});
			},
			formChangeHandle(val) {
				this.pdata = Object.assign(this.pdata,val);
				//传递数据到父组件里
				this.$emit('click', this.pdata);
				//触发外部的事件，以标志着本表单已经准备完毕
				this.$emit('formchange', this.pdata);
			},
			saveDialog(){
				//不等于大客户
				// if(types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER != this.pdata.dealerType){
				// 	this.$refs.FormPanel.SUBMIT_FORM();
				// 	return false;
				// }
				//如果是大客户，则验证其‘资金占比总和是否是100’
				if(this.verificationEquityRatio()){
					this.$refs.FormPanel.SUBMIT_FORM();
				}else{
					try{
						//(滚动到xx模块)需要父页面的this
						// this.$parent.$el.children[0].children[4].click();
					}catch (e){
						// console.error("this.$el.children[0].children[4],出现错误，请检查",e,this.$el);
					}
					this.$notify.warning({title: '提示信息',message:'公司信息占比总和不等于100，请核对!'});
				}
			},
			//验证‘资金占比’是否为100
			verificationEquityRatio(){
				let equityRatio1 = this.$refs.FormPanel.form.equityRatio1;
				let equityRatio2 = this.$refs.FormPanel.form.equityRatio2;
				let equityRatio3 = this.$refs.FormPanel.form.equityRatio3;
				let equityRatio = this.$refs.FormPanel.form.equityRatio;
				if(!equityRatio1 && !equityRatio2 && !equityRatio3 && !equityRatio){
					return true;
				}
				//
				this.$refs.FormPanel.excuteFormData(this.$refs.FormPanel.form);
				equityRatio1 = parseFloat(equityRatio1);
				equityRatio2 = parseFloat(equityRatio2);
				equityRatio3 = parseFloat(equityRatio3);
				equityRatio = parseFloat(equityRatio);

				//如果存在NaN或Infinity则置为0
				if(isNaN(equityRatio1) || equityRatio1 === Infinity){
					equityRatio1 = 0;
				}
				if(isNaN(equityRatio2) || equityRatio2 === Infinity){
					equityRatio2 = 0;
				}
				if(isNaN(equityRatio3) || equityRatio3 === Infinity){
					equityRatio3 = 0;
				}
				if(isNaN(equityRatio) || equityRatio === Infinity){
					equityRatio = 0;
				}

				// if(isNaN(equityRatio1) || isNaN(equityRatio2) || isNaN(equityRatio3) || isNaN(equityRatio)){
				//	 return false;
				// }
				if((equityRatio1 + equityRatio2 + equityRatio3 + equityRatio) === 100){
					return true;
				}

				return false;
			},
			//经销商类型变更时
			changeDealerType(value){
				if(types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value){
					// this.stockEnabled();
				}else{
					this.stockDisabled();
				}
				// this.refreshFormRules();
			},
			//禁用股东信息部分(只去除验证)
			stockDisabled(){
				vm.stockDisabled(this.initForm,false);
				vm.clearValidate(this.$refs.FormPanel);
			},
			//启用股东信息部分(只启用验证)
			stockEnabled(){
				vm.stockEnabled(this.initForm,false);
				vm.clearValidate(this.$refs.FormPanel);
			},
			//停用，未完善
			//type:float,precision:2
			yz(rule, value, callback){
				if(!value){
					return ;
				}
				let msg = rule.message || "请填写正确的数字";
				//数字类型
				let type = rule.type || 'int';//int,float
				//小数位
				let precision = rule.precision;
				//整数长度
				let maxlength = rule.maxlength;
				//最大值
				let max = rule.max;
				//最小值
				let min = rule.min;

				//
				if(type === 'float'){
					//如果前后出现.则认定为不数字
					if(value.substring(0,1) == '.' || value.substring(value.length - 1)){
						callback(new Error(msg + ",小数点前后应该有数字"));
						return ;
					}else{

					}
				}else if(type === 'int'){
					//如果没有点则下一步验证
					if(String(value).indexOf(".") == -1){
						let numberValue = Number(value);
						if(isNaN(numberValue)){
							callback(new Error(msg + ",不是正确的数字"));
							return ;
						}else{
							//判断长度是否超长
							if(maxlength){
								if(!(String(value).length <= maxlength)){
									callback(new Error(msg + ",长度不能比[" + maxlength + "]大"));
									return ;
								}
							}
							//最大最小都存在
							if(max && min){
								//比最小值小，或比最大值大
								if(!(min <= numberValue && numberValue <= max)){
									callback(new Error(msg + ",在[" + min + "-" + max + "]之间"));
									return ;
								}
							}else if(max){
								//判断是否超过最大值
								if(!(numberValue <= max)){
									callback(new Error(msg + ",不能比[" + max + "]大"));
									return ;
								}
							}else if(min){
								//判断是否小于最小值
								if(!(min <= numberValue)){
									callback(new Error(msg + ",不能比[" + min + "]小"));
									return ;
								}
							}

						}
					}else{
						callback(new Error(msg + ",整数"));
						return ;
					}
				}
				callback();
			},
			//刷新验证内容
			// refreshFormRules(){
			// 	vm.refreshFormRules(this,this.$refs.FormPanel,this.initForm);
			// },
			//搜索数据
			initFormFun() {
				return [
					{type:'INPUT',label:'股东一',fieldName:'shareholderName1',initialValue:'',maxlength:100,placeholder:'',},
					{type:'INPUT',label:'资金',fieldName:'inputAmount1',initialValue:'',maxlength:15,placeholder:'',
						unitRender:() => {
							return<span>万</span>
						},
					},
					{type:'INPUT',label:'占比',fieldName:'equityRatio1',initialValue:'',maxlength:3,placeholder:'',
						unitRender:() => {
							return<span>%</span>
						},
					},
					{type:'INPUT',label:'股东二',fieldName:'shareholderName2',initialValue:'',maxlength:100,placeholder:'',},
					{type:'INPUT',label:'资金',fieldName:'inputAmount2',initialValue:'',maxlength:15,placeholder:'',
						unitRender:() => {
							return<span>万</span>
						},
					},
					{type:'INPUT',label:'占比',fieldName:'equityRatio2',initialValue:'',maxlength:3,placeholder:'',
						unitRender:() => {
							return<span>%</span>
						},
					},
					{type:'INPUT',label:'股东三',fieldName:'shareholderName3',initialValue:'',maxlength:100,placeholder:'',},
					{type:'INPUT',label:'资金',fieldName:'inputAmount3',initialValue:'',maxlength:15,placeholder:'',
						unitRender:() => {
							return<span>万</span>
						},
					},
					{type:'INPUT',label:'占比',fieldName:'equityRatio3',initialValue:'',maxlength:3,placeholder:'',
						unitRender:() => {
							return<span>%</span>
						},
					},
					{type:'INPUT',label:'其他',fieldName:'shareholderName',initialValue:'',maxlength:100,placeholder:'',},
					{type:'INPUT',label:'资金',fieldName:'inputAmount',initialValue:'',maxlength:15,placeholder:'',
						unitRender:() => {
							return<span>万</span>
						},
					},
					{type:'INPUT',label:'占比',fieldName:'equityRatio',initialValue:'',maxlength:3,placeholder:'',
						unitRender:() => {
							return<span>%</span>
						},
					}
				];
			},
		}
	};
</script>